package part2;

import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.TransferHandler;

public class ImageTransferHandler extends TransferHandler implements Transferable{

	/**
	 * Data members
	 * - the serialVersionUID is needed to get rid of a compiler warning
	 * - 
	 */
	private static final long serialVersionUID = 1L;
	private Image xferImage; // image being transferred
	private final DataFlavor ourflavors[] = { DataFlavor.imageFlavor }; // we only have 1 flavor
	
	/**
	 * getSourceActions - this is a method that is invoked for the drag gesture.
	 *  The return values are usually static ints from the TransferHandler class:
	 *  COPY == we are making a copy of the source and will put that into the destination
	 *  MOVE == we are removing something from the source (cut) and will be putting it (pasting) into the destination
	 *  LINK == a link to the source will be put into the destination
	 *  COPY_OR_MOVE 
	 */
	public int getSourceActions(JComponent c) {
		System.out.println(" getSourceActions ");
	    return MOVE;
	}
	
	/**
	 * createTransferable
	 *  - returns this object if we have a drag source that is a JLabel with an image in it.
	 * 
	 */
	public Transferable createTransferable(JComponent comp) {
		System.out.println(" createTransferable ");
		xferImage = null;

		if (comp instanceof JLabel) {
			JLabel label = (JLabel) comp;
			Icon icon = label.getIcon();
			if (icon instanceof ImageIcon) {
				xferImage = ((ImageIcon) icon).getImage();
				return this;
			}
		} 
		return null;
	}
	
	/**
	 * exportDone - this is called when the export is done (dropped)
	 */
	public void exportDone(JComponent comp, Transferable t, int action) {
		System.out.println(" exportDone ");
	    if (action == MOVE) {
	        // Remove selection from source
			if (comp instanceof JLabel) {
				JLabel label = (JLabel) comp;
				label.setIcon( null );				
			}
	    }
	}
	
	/**
	 * canImport - called during the drag, returns true if the component can accept
	 * i.e. if one of the flavors of the component matches one of ours
	 */
	public boolean canImport(JComponent comp, DataFlavor flavors[]) {
		System.out.println(" canImport ");
		if (!(comp instanceof JLabel))
			return false;

		for (DataFlavor compf : flavors)
			for (DataFlavor ours : ourflavors)
				if (compf.equals(ours))
					return true;
		return false;
	  }
	
	/**
	 * importData - called on a successful drop
	 */
	public boolean importData(JComponent comp, Transferable t) {
		System.out.println(" importData ");
		if (comp instanceof JLabel) {
			JLabel label = (JLabel) comp;
			if (t.isDataFlavorSupported(ourflavors[0]))
				try {
					xferImage = (Image) t.getTransferData(ourflavors[0]);
					label.setIcon(new ImageIcon(xferImage));
					return true;
				} catch (UnsupportedFlavorException ignored) {
					
				} catch (IOException ignored) {
					
				}
		}
		return false;
	  }
	
	// The next 3 methods are needed by the Transferable interface
	/**
	 * getTransferData - if the client wants an image, return the xferImage object. 
	 */
	public Object getTransferData(DataFlavor arg0)
			throws UnsupportedFlavorException, IOException {
		System.out.println(" getTransferData ");
		if ( isDataFlavorSupported(arg0) )
			return xferImage;
		return null;
	}

	/**
	 * getTransferDataFlavors
	 *  - yes, it returns an array data member but the "ourflavors" array is a final.
	 */
	public DataFlavor[] getTransferDataFlavors() {
		System.out.println(" getTransferDataFlavors ");
		return ourflavors;
	}

	/**
	 * isDataFlavorSupported - searches the ourflavors array to
	 *  see if the argument is one that we support.
	 */
	public boolean isDataFlavorSupported(DataFlavor arg0) {
		System.out.println(" isDataFlavorSupported ");
		for (DataFlavor f : ourflavors)
			if (f.equals(arg0))
				return true;
		return false;
	}	
}